package org.panda.tcmoc.persistence.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.panda.tcmoc.domain.SpProduct;
import org.panda.tcmoc.domain.User;
import org.panda.tcmoc.persistence.dao.UserDao;
import org.panda.tcmoc.persistence.db.DbHelper;
import org.panda.tcmoc.persistence.db.impl.DbHelperImpl;

public class UserDaoImpl implements UserDao {
	private DbHelper _dbHelper = new DbHelperImpl();
	private Logger logger = Logger.getLogger(UserDaoImpl.class);

	public User findUserById(String userId) {
		Connection conn = _dbHelper.getConn();
		String sql = "select * from User where Id=?";
		User user = new User();
		try {
			PreparedStatement st = conn.prepareStatement(sql);
			st.setString(1, userId);
			st.execute();
			ResultSet rs = st.getResultSet();
			while (rs.next()) {
				user = this.fillDomainByResult(rs);
			}
			rs.close();
			st.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return user;
	}

	private User fillDomainByResult(ResultSet rs) throws SQLException {
		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));
		return user;
	}
}
